<?php


namespace App\Controller\Api;

use App\Model\Enum\ChannelInfoStatusEnum;
use App\Service\Dispatch\ChannelDispatchService;
use App\Service\RiskCheck\CommonRiskCheckService;
use App\Service\RiskRuleInfoService;
use App\Service\RiskRuleParamService;
use App\Service\SyncService;
use App\Utils\CustomUtils;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Logger\LoggerFactory;
use Hyperf\Utils\Arr;

/**
 * Class SyncController
 * 数据同步/缓存刷新
 *
 * @package App\Controller\Api
 * @Controller(prefix="/api/sync")
 */
class SyncController extends BaseController {

    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $logger;

    /**
     * @Inject()
     * @var CommonRiskCheckService
     */
    protected $CommonRiskCheckService;

    /**
     * @Inject()
     * @var RiskRuleInfoService
     */
    protected $RiskRuleInfoService;

    /**
     * @Inject()
     * @var SyncService
     */
    protected $SyncService;

    /**
     * @Inject()
     * @var ChannelDispatchService
     */
    protected $ChannelDispatchService;

    public function __construct (LoggerFactory $loggerFactory) {
        // 第一个参数对应日志的 name, 第二个参数对应 config/autoload/logger.php 内的 key
        $this->logger = $loggerFactory->get('log', 'default');
    }

    /**
     * 当规则参数更新
     *
     * @param RequestInterface $request
     * @PostMapping(path="riskRuleParam")
     *
     * @return array
     */
    public function riskRuleParam (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'paramId' => 'required',
        ]);

        $paramId = $request->post('paramId');

        return $this->SyncService->syncRiskRuleParam($paramId);
    }

    /**
     * 当规则明细更新
     *
     * @param RequestInterface $request
     * @PostMapping(path="riskRuleInfo")
     *
     * @return array
     */
    public function riskRuleInfo (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'ruleId' => 'required',
        ]);

        $ruleId = $request->post('ruleId');

        return $this->SyncService->syncRiskRuleInfo($ruleId);
    }


    /**
     * 当风控规则集合更新
     *
     * @param RequestInterface $request
     * @PostMapping(path="riskRuleCollection")
     *
     * @return array
     */
    public function riskRuleCollection (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'collectionId' => 'required',
        ]);

        $collectionId = $request->post('collectionId');

        return $this->SyncService->syncRiskRuleCollection($collectionId);
    }

    /**
     * 当商户白名单关联更新
     *
     * @param RequestInterface $request
     * @PostMapping(path="riskRuleWhiteTextMchRef")
     *
     * @return array
     */
    public function riskRuleWhiteTextMchRef (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'mchNo' => 'required',
        ]);

        $mchNo = $request->post('mchNo');

        return $this->SyncService->syncRiskRuleWhiteTextMchRef($mchNo);
    }


    /**
     * 当全局风控关联更新
     *
     * @param RequestInterface $request
     * @PostMapping(path="riskRuleGlobalRef")
     *
     * @return array
     */
    public function riskRuleGlobalRef (RequestInterface $request) {

        $this->_validOrFail($request->all(), []);

        return $this->SyncService->syncRuleGlobalRef();
    }


    /**
     * 当通道风控关联更新
     *
     * @param RequestInterface $request
     * @PostMapping(path="riskRuleChannelRef")
     *
     * @return array
     */
    public function riskRuleChannelRef (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'channelNo' => 'required',
        ]);

        $channelNo = $request->post('channelNo');

        return $this->SyncService->syncRiskRuleChannelRef($channelNo);
    }


    /**
     * 当商户风控关联更新
     *
     * @param RequestInterface $request
     * @PostMapping(path="riskRuleMchRef")
     *
     * @return array
     */
    public function riskRuleMchRef (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'mchNo' => 'required',
        ]);

        $mchNo = $request->post('mchNo');

        return $this->SyncService->syncRiskRuleMchRef($mchNo);
    }

    /**
     * 当商户可疑风控关联更新
     *
     * @param RequestInterface $request
     * @PostMapping(path="riskRuleSuspiciousMchRef")
     *
     * @return array
     */
    public function riskRuleSuspiciousMchRef (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'mchNo' => 'required',
        ]);

        $mchNo = $request->post('mchNo');

        return $this->SyncService->syncRiskRuleSuspiciousMchRef($mchNo);
    }

    /**
     * 当白名单参数更新
     *
     * @param RequestInterface $request
     * @PostMapping(path="whiteTextParam")
     *
     * @return array
     */
    public function whiteTextParam (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'paramId' => 'required',
        ]);

        $paramId = $request->post('paramId');

        return $this->SyncService->syncWhiteTextParam($paramId);
    }

    /**
     * 当白名单集合更新
     *
     * @param RequestInterface $request
     * @PostMapping(path="whiteText")
     *
     * @return array
     */
    public function whiteText (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'collectionId' => 'required',
        ]);

        $collectionId = $request->post('collectionId');

        return $this->SyncService->syncWhiteText($collectionId);
    }

    /**
     * 当通道/通道集合更新
     *
     * @param RequestInterface $request
     * @PostMapping(path="channelCollection")
     *
     * @return array
     */
    public function channelCollection (RequestInterface $request) {

        $this->_validOrFail($request->all(), [// 'conn' => 'required',
        ]);

        $conn = $request->post('conn', []);

        return $this->SyncService->syncChannelCollection($conn);
    }

    /**
     * 当商户关联通道集合更新
     *
     * @param RequestInterface $request
     * @PostMapping(path="channelCollectionMchRef")
     *
     * @return array
     */
    public function channelCollectionMchRef (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'mchNo' => 'required',
        ]);

        $mchNo = $request->post('mchNo');

        return $this->SyncService->syncChannelCollectionMchRef($mchNo);
    }

    /**
     * 通道开启/关闭
     *
     * @param RequestInterface $request
     * @PostMapping(path="setChannelStatus")
     *
     * @return array
     */
    public function setChannelStatus (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'mchNo'     => 'required',
            'channelNo' => 'required',
            'status'    => 'required',
        ]);

        $mchNo     = $request->post('mchNo');
        $channelNo = $request->post('channelNo');
        $status    = $request->post('status', ChannelInfoStatusEnum::ON);

        // 限制枚举类型
        $status = ChannelInfoStatusEnum::born($status)->value();

        return $this->SyncService->setMchChannelStatus($mchNo, $channelNo, $status);
    }

    /**
     * 通道限额使用情况
     *
     * @param RequestInterface $request
     * @PostMapping(path="getChannelCost")
     *
     * @return array
     */
    public function getChannelCost (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'channelNo' => 'required',
        ]);

        $channelNo = $request->post('channelNo');

        return $this->SyncService->getChannelCost($channelNo);
    }

    /**
     * 商户-通道限额使用情况
     * @param RequestInterface $request
     * @PostMapping(path="getMchChannelCost")
     *
     * @return array
     */
    public function getMchChannelCost (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'mchNo'     => 'required',
            'channelNo' => 'required',
        ]);

        $mchNo     = $request->post('mchNo');
        $channelNo = $request->post('channelNo');

        return $this->SyncService->getMchChannelCost($mchNo, $channelNo);
    }


    /**
     * 上送银行失败,限额回滚
     *
     * @param RequestInterface $request
     * @PostMapping(path="recoverChannelLimit")
     *
     * @return array
     */
    public function recoverChannelLimit (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'mchNo'                => 'required',
            'channelNo'            => 'required',
            'channelLimitAmount'   => 'required',
            'year'                 => 'required',
            'month'                => 'required',
            'day'                  => 'required',
        ]);

        $params = Arr::only($request->post(), [
            'mchNo',
            'channelNo',
            'channelLimitAmount',
            'year',
            'month',
            'day',
        ]);

        return $this->SyncService->rollbackChannelLimit($params);
    }
}
